package web

import (
	"blockchain_tool/logger"
	"bytes"
	"io"
	"net/http"
)

type RequestInfo struct {
	Url            string `json:"urlPath"`
	RequestHeaders map[string]string
}

func GetRequestInfo(path string) *RequestInfo {
	// 配置请求信息
	var requestInfo RequestInfo
	requestInfo.Url = path
	// TODO: 配置请求头
	return &requestInfo
}
func PostRequestInfo(url, cookie string) *RequestInfo {
	// 配置请求信息
	var requestInfo RequestInfo
	requestInfo.Url = url
	// TODO: 配置请求头
	return &requestInfo
}

func GetHttp(requestInfo *RequestInfo) ([]byte, error) {
	url := requestInfo.Url
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		logger.Log.Printf("Create New Request failed: %v\n", err)
		return nil, err
	}
	// 配置请求头
	req.Header.Set("Content-Type", "application/json")
	for k, v := range requestInfo.RequestHeaders {
		req.Header.Set(k, v)
	}
	// 发送请求
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		logger.Log.Printf("Send Request failed: %v\n", err)
		return nil, err
	}
	defer resp.Body.Close()
	// 读取响应
	body, _ := io.ReadAll(resp.Body)
	return body, nil
}

func PostHttp(requestInfo *RequestInfo, requestBody []byte) ([]byte, error) {
	url := requestInfo.Url
	req, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody))
	if err != nil {
		logger.Log.Printf("Create New Request failed: %v\n", err)
		return nil, err
	}
	// 配置请求头
	req.Header.Set("Content-Type", "application/json")
	for k, v := range requestInfo.RequestHeaders {
		req.Header.Set(k, v)
	}

	// 发送请求
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		logger.Log.Printf("Send Request failed: %v\n", err)
		return nil, err
	}
	defer resp.Body.Close()
	// 读取响应
	body, _ := io.ReadAll(resp.Body)
	return body, nil
}

// http/2 版本
// func PostHttp_v2(requestInfo *RequestInfo, requestBody []byte) ([]byte, error) {
// 	url := requestInfo.Url
// 	req, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody))
// 	if err != nil {
// 		logger.Log.Printf("Create New Request failed: %v\n", err)
// 		return nil, err
// 	}
// 	// 配置请求头
// 	req.Header.Set("Content-Type", "application/json")
// 	for k, v := range requestInfo.RequestHeaders {
// 		req.Header.Set(k, v)
// 	}

// 	// 初始化HTTP客户端
// 	// client := &http.Client{}
// 	client := &http.Client{
// 		Transport: &http2.Transport{
// 			TLSClientConfig: &tls.Config{
// 				// InsecureSkipVerify: true, // 跳过证书验证，仅用于测试
// 			},
// 		},
// 	}

// 	// 发送请求
// 	resp, err := client.Do(req)
// 	if err != nil {
// 		logger.Log.Printf("Send Request failed: %v\n", err)
// 		return nil, err
// 	}
// 	defer resp.Body.Close()
// 	// 读取响应
// 	body, _ := io.ReadAll(resp.Body)
// 	return body, nil
// }

// client复用模式
func PostHttp_v3(client *http.Client, requestInfo *RequestInfo, requestBody []byte) ([]byte, error) {
	url := requestInfo.Url
	req, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody))
	if err != nil {
		logger.Log.Printf("Create New Request failed: %v\n", err)
		return nil, err
	}
	// 配置请求头
	req.Header.Set("Content-Type", "application/json")
	for k, v := range requestInfo.RequestHeaders {
		req.Header.Set(k, v)
	}

	// 发送请求
	resp, err := client.Do(req)
	if err != nil {
		logger.Log.Printf("Send Request failed: %v\n", err)
		return nil, err
	}
	defer resp.Body.Close()
	// 读取响应
	body, _ := io.ReadAll(resp.Body)
	return body, nil
}

func PostHttp_test(client *http.Client, requestInfo *RequestInfo, requestBody []byte) ([]byte, error) {
	url := requestInfo.Url
	req, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody))
	if err != nil {
		logger.Log.Printf("Create New Request failed: %v\n", err)
		return nil, err
	}
	// 配置请求头
	// req.Header.Set("Content-Type", "application/json")
	// for k, v := range requestInfo.RequestHeaders {
	// 	req.Header.Set(k, v)
	// }
	req.Header.Set("Accept", "application/json, text/plain, */*")
	req.Header.Set("Accept-language", "ja")
	req.Header.Set("Baggage", "sentry-environment=online,sentry-release=235b5bc66d60b624c78d570ce6f2bcf5476935f7,sentry-transaction=tradingView-component-load,sentry-public_key=8aeef93d782e4ef5ae75634bfee9b590,sentry-trace_id=301c81a61ea84e518a957937e9e0f807,sentry-sample_rate=0.02")
	req.Header.Set("Content-type", "application/json;charset=UTF-8")
	req.Header.Set("Cookie", "fingerprint-1732364809684-13659.900000095367-0.3901659447813326=true; terminalCode-1732364809685-13661.5-0.9899125630235073=true; theme=black; _ga_clientid=1019972852.1732364803; _ga_sessionid=1732364803; _dx_kvani5r=1818f6810091d12f9c412bb4a60ef8d8b483fd73e9343c913c482fc575b0489b9d8122aa; afUserId=06d0cfcd-b9a5-4b20-9ae4-ca77034b47e5-p; AF_SYNC=1732364828986; _ym_uid=1732364829113186237; _ym_d=1732364829; __adroll_fpc=9d74b20d553a231728d63ce640b06407-1732364832151; bitget_lang=zh-CN; OptanonAlertBoxClosed=Sat%20Nov%2023%202024%2021:27:41%20GMT+0900%20(%E6%97%A5%E6%9C%AC%E6%A8%99%E6%BA%96%E6%99%82); OptanonConsent=isMarketing=1&isStatistic=1; _cfuvid=WPw17b2BkdRfY4fvQ7np35y4XwYzEJSvDGCQzR5w8FU-1732364867926-0.0.1.1-604800000; brand_simulation_trade=false; captcha_v4_user=aa5522d6720d4f5ab5f94618cc403ecf; bt_rtoken=upex:session:id:54f5487cad7da462b77990ba05ad7e3726fbdb7885b6cbd27a0b0fbe81ae3233; bt_uid=0431FAF8DE2905E6D536D31CEA236882; bt_sessonid=42a81302-b94c-4b27-a3e3-1bf231b8bd77; bt_newsessionid=eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiIyMTczYTZlNi1lODZiLTQ1OTMtYWIwZC03ZGI4ZTA3NzI2MTI1OTg4MTczNjEiLCJ1aWQiOiIvQXRtYUxUZm4vT05UeVZqTFVGbExRPT0iLCJzdWIiOiJmYW4qKipvbSIsImlwIjoiUUd3MUR6MmltV3RoQk5RdFo0S0cxUT09IiwiZGlkIjoibHZzOGxzUEpubWZiM1Z4Z0E3aWZ3SU9KdDNRK2ZMbG1QQnBhOWsvZ01wUjEyUklzWlFLTTVIQmtTNHlUUmhuVyIsInN0cyI6MCwiaWF0IjoxNzMyMzY3ODA3LCJleHAiOjE3MzI3OTk4MDcsInB1c2hpZCI6ImRka1NMR1VDak9Sd1pFdU1rMFlaMWc9PSIsImlzcyI6InVwZXgifQ.afBLK5u5qp48Htb4uNV5yar05GhcnfL6inPlNy6SaIk; BITGET_LOCAL_COOKIE={%22bitget_lang%22:%22zh-CN%22%2C%22bitget_unit%22:%22CNY%22%2C%22bitget_showasset%22:true%2C%22bitget_theme%22:%22black%22%2C%22bitget_layout%22:%22right%22%2C%22bitget_valuationunit%22:1%2C%22bitget_valuationunitandfiat%22:1%2C%22bitgt_login%22:false}; _ga_B8RNGYK5MS=GS1.1.1732367810.1.0.1732367810.0.0.0; __ar_v4=R3652JF77NH6ZC5OBZHWIH%3A20241123%3A4%7C2WBEMJJKHFG5PLSUZ7B5OY%3A20241123%3A4; _ga=GA1.2.1019972852.1732364803; _gid=GA1.2.818659172.1732540516; _ga_9XQG87E8PF=GS1.2.1732540520.1.0.1732540520.0.0.0; _ga_Z8Q93KHR0F=GS1.1.1732541162.3.0.1732541162.60.0.0; dy_token=674486c70CxenqJ9YJvLJFXtCmaBbPKrZJzzQXs1; __cf_bm=uB.AQONfxSno9z_6Q6HSBfxfdgwegPZG3p8pRxMziVI-1732578756-1.0.1.1-5kzU7pe1RXoYHM3BiaE8x5FAKggr1q96reEIedMpavK1GGCGIf_ImR7QhIZEyj4plOOuYk_zJIolpIGzjK82ww")
	req.Header.Set("Deviceid", "1ec5066fa1519a822cce7889b3049d6f")
	req.Header.Set("Dy-token", "674486c70CxenqJ9YJvLJFXtCmaBbPKrZJzzQXs1")
	req.Header.Set("Language", "zh_CN")
	req.Header.Set("Locale", "zh_CN")
	req.Header.Set("Origin", "https://www.bitget.com")
	req.Header.Set("Priority", "u=1, i")
	req.Header.Set("Sec-ch-ua", `"Chromium";v="130", "Google Chrome";v="130", "Not?A_Brand";v="99"`)
	req.Header.Set("Sec-ch-ua-arch", `"x86"`)
	req.Header.Set("Sec-ch-ua-bitness", `"64"`)
	req.Header.Set("Sec-ch-ua-full-version", `"130.0.6723.59"`)
	req.Header.Set("Sec-ch-ua-full-version-list", `"Chromium";v="130.0.6723.59", "Google Chrome";v="130.0.6723.59", "Not?A_Brand";v="99.0.0.0"`)
	req.Header.Set("Sec-ch-ua-mobile", "?0")
	req.Header.Set("Sec-ch-ua-model", `""`)
	req.Header.Set("Sec-ch-ua-platform", `"Windows"`)
	req.Header.Set("Sec-ch-ua-platform-version", `"15.0.0"`)
	req.Header.Set("Sec-fetch-dest", "empty")
	req.Header.Set("Sec-fetch-mode", "cors")
	req.Header.Set("Sec-fetch-site", "same-origin")
	req.Header.Set("Sentry-trace", "301c81a61ea84e518a957937e9e0f807-bcdf5770618ec00b-0")
	req.Header.Set("Terminalcode", "adba6008a351eba4b52fc8c7efe44a94")
	req.Header.Set("Terminaltype", "1")
	req.Header.Set("Uhti", "w1732578800629d81a425f253")
	req.Header.Set("User-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36")
	req.Header.Set("Website", "mix")

	// 发送请求
	resp, err := client.Do(req)
	if err != nil {
		logger.Log.Printf("Send Request failed: %v\n", err)
		return nil, err
	}
	defer resp.Body.Close()
	// 读取响应
	body, _ := io.ReadAll(resp.Body)
	return body, nil
}
